﻿//链表节点
using System.Xml.Linq;

public class Node<T>
{
    public Node<T> Next { get; set; }
    public T Data { get; set; }
    public Node(T t)
    {
        Next = null;
        Data = t;
    }
}

//泛型链表类
public class GenericList<T>
{
    private Node<T> head;
    private Node<T> tail;
    public Node<T> Head
    {
        get => head;
    }

    public GenericList()
    {
        tail = head = null;
    }


    public void Add(T t)
    {
        Node<T> n = new Node<T>(t);

        if (tail == null)
        {
            head = tail = n;
        }
        else
        {
            tail.Next = n;
            tail = n;
        }
    }

    public void ForEach(Action<T> action)
    {
        Node<T> node = head;
        while (node != null)
        {
            action(node.Data);
            node = node.Next;
        }
    }

}

class List
{
   static void Main(string[] args)
    {
        ////整型List
        //GenericList<int> intlist = new GenericList<int>();
        //for (int x = 0; x < 10; x++)
        //{
        //    intlist.Add(x);
        //}
        //for (Node<int> node = intlist.Head; node != null; node = node.Next)
        //{
        //    Console.Write(node.Data+' ');
        //}
        ////字符串型List
        //GenericList<string> strList = new GenericList<string>();
        //for (int x = 0; x < 10; x++)
        //{
        //    strList.Add("str" + x);
        //}
        //for (Node<string> node = strList.Head;
        //node != null; node = node.Next)
        //{
        //    Console.WriteLine(node.Data);
        //}

        GenericList<int> list = new GenericList<int>();
        list.Add(1);
        list.Add(7);
        list.Add(3);
        list.Add(2);
        list.Add(9);

        int sum = 0;
        int max = int.MinValue;
        int min = int.MaxValue;

        list.ForEach(n =>
        {
            Console.Write(n + " ");
            sum += n;
            if (n > max)
            {
                max = n;
            }
            if (n < min)
            {
                min = n;
            }
        });

        Console.WriteLine("");
        Console.WriteLine("Sum: " + sum);
        Console.WriteLine("Max: " + max);
        Console.WriteLine("Min: " + min);

    }
}